import { login } from '@/api/oauth2/oauth2'
import { getOAuth2AccessToken, getOAuth2RefreshToken, setOAuth2AccessToken, setOAuth2RefreshToken } from '@/utils/auth'

const oauth2 = {
    state: {
        accessToken: getOAuth2AccessToken(),
        refreshToken: getOAuth2RefreshToken()
    },

    mutations: {
        SET_ACCESS_TOKEN: (state, token) => {
            state.token = token
        },
        SET_REFRESH_TOKEN: (state, name) => {
            state.name = name
        }
    },

    actions: {
        OAuth2Login({ commit }, userInfo) {
            const username = userInfo.username.trim()
            const password = userInfo.password
            const code = userInfo.code
            const uuid = userInfo.uuid
            return new Promise((resolve, reject) => {
                login(username, password).then(res => {
                    console.log('oauth2登录结果', res)
                    setOAuth2AccessToken(res.data.access_token)
                    setOAuth2RefreshToken(res.data.refresh_token)
                    // commit('SET_TOKEN', res.token)
                    resolve()
                }).catch(error => {
                    reject(error)
                })
            })
        }
    }
}

export default oauth2
